﻿using AutoMapper;
using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.ReadAPI.Applications.Commands.Users;
using B.S.Meeting.ReadAPI.Dtos;
using MediatR;
using System.Linq;

namespace B.S.Meeting.ReadAPI.Applications.CommandHandlers.Users
{
    public class GetUserCommandHandler : IRequestHandler<GetUserCommand, APIResult<APIPaging<List<UserDto>>>>
    {
        IBaseRepository<User> uRep;
        IMapper mapper;
        ILogger<GetUserCommandHandler> logger;
        public GetUserCommandHandler(IBaseRepository<User> uRep, IMapper mapper, ILogger<GetUserCommandHandler> logger)
        {
            this.uRep = uRep;
            this.mapper = mapper;
            this.logger = logger;
        }
        public async Task<APIResult<APIPaging<List<UserDto>>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = uRep.GetAll().Where(x => x.IsDel == false);
                //查询用户名
                if (!string.IsNullOrEmpty(request.UserName))
                {
                    list = list.Where(x => x.UserName.Contains(request.UserName));
                }
                //查询用户编号
                if (request.IDnumber > 0)
                {
                    list=list.Where(x => x.IDnumber == request.IDnumber);
                }
                //查询账号状态
                if (request.Userstatus != null)
                {
                    list = list.Where(x => x.Userstatus == request.Userstatus);
                }
                //查询会员等级
                if (!string.IsNullOrEmpty(request.MemberLevel))
                {
                    list = list.Where(x => x.MemberLevel.Contains(request.MemberLevel));
                }

                // 根据消费金额范围进行过滤
                if (!string.IsNullOrEmpty(request.AmountRange))
                {
                    switch (request.AmountRange)
                    {
                        case "100元以下":
                            list = list.Where(x => x.Amount < 100);
                            break;
                        case "100-500元":
                            list = list.Where(x => x.Amount >= 100 && x.Amount <= 500);
                            break;
                        case "500-1000元":
                            list = list.Where(x => x.Amount >= 500 && x.Amount <= 1000);
                            break;
                        case "1000-5000元":
                            list = list.Where(x => x.Amount >= 1000 && x.Amount <= 5000);
                            break;
                        case "5000元以上":
                            list = list.Where(x => x.Amount > 5000);
                            break;
                            // 可以根据需要添加更多的范围条件
                    }
                }

                int totalCount = list.Count();
                int totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                list = list.OrderBy(x => x.UserId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
                var uDto = mapper.Map<List<UserDto>>(list);
                return new APIResult<APIPaging<List<UserDto>>>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "获取用户列表成功",
                    Data = new APIPaging<List<UserDto>>
                    {
                        TotalCount = totalCount,
                        TotalPage = totalPage,
                        Data = uDto
                    }
                };
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, "获取用户列表失败");
                throw;
            }
        }
    }
}
